package com.java.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.event.AncestorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderTest extends JFrame implements ChangeListener{
//	public static int red,green,blue;
	private JLabel redLabel,greenLabel,blueLabel,redValue,greenValue,blueValue;
	private JSlider redSlider,greenSlider,blueSlider;
	private JTextArea colorArea;
	public SliderTest(){
		super("ScrollTest!!");
		redLabel=new JLabel("   Red:");
		greenLabel=new JLabel("Green:");
		blueLabel=new JLabel("  Blue:");
		redValue=new JLabel("127");
		greenValue=new JLabel("127");
		blueValue=new JLabel("127");
		redSlider=new JSlider(0,255);
		greenSlider=new JSlider(0,255);
		blueSlider=new JSlider(0,255);
		colorArea=new JTextArea(10,15);
		colorArea.setEditable(false);
		colorArea.setBackground(new Color(127,127,127));
		initial();
	}
	public void initial(){
		JPanel centerPanel=new JPanel();
		JPanel southPanel=new JPanel();
		this.add(centerPanel,BorderLayout.CENTER);
		centerPanel.setLayout(new FlowLayout());
		centerPanel.add(redLabel);
		centerPanel.add(redSlider);
		centerPanel.add(redValue);
		centerPanel.add(greenLabel);
		centerPanel.add(greenSlider);
		centerPanel.add(greenValue);
		centerPanel.add(blueLabel);
		centerPanel.add(blueSlider);
		centerPanel.add(blueValue);
		this.add(southPanel,BorderLayout.SOUTH);
		southPanel.setLayout(new FlowLayout());
		southPanel.add(colorArea);
	}
	public void showMe(){
		addEventHandler();
		this.setSize(300, 250);
//		this.pack();
		
		this.setResizable(false);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
//	public void addEventHandler(){
//		
//		redSlider.addChangeListener(new ChangeListener(){
//			
//			public void stateChanged(ChangeEvent e) {
//				SliderTest.red=redSlider.getValue();
//				Integer i=SliderTest.red;
//				redValue.setText(i.toString());
//				colorArea.setBackground(new Color(red,green,blue));
//			}
//			
//		});
//		greenSlider.addChangeListener(new ChangeListener(){
//
//			public void stateChanged(ChangeEvent e) {
//				SliderTest.green=greenSlider.getValue();
//				Integer i=SliderTest.green;
//				greenValue.setText(i.toString());
//				colorArea.setBackground(new Color(red,green,blue));
//			}
//			
//		});
//		blueSlider.addChangeListener(new ChangeListener(){
//
//			public void stateChanged(ChangeEvent e) {
//				SliderTest.blue=blueSlider.getValue();
//				Integer i=SliderTest.blue;
//				blueValue.setText(i.toString());
//				colorArea.setBackground(new Color(red,green,blue));
//			}
//			
//		});
//	}
	public void addEventHandler(){
		redSlider.addChangeListener(this);
		greenSlider.addChangeListener(this);
		blueSlider.addChangeListener(this);
	}
	public static void main(String[] args){
		new SliderTest().showMe();
	}
	public void stateChanged(ChangeEvent e) {
		int red,green,blue;
		red=redSlider.getValue();
		green=greenSlider.getValue();
		blue=blueSlider.getValue();
		redValue.setText(red+"");
		greenValue.setText(green+"");
		blueValue.setText(blue+"");
		Color c=new Color(red,green,blue);
		colorArea.setBackground(c);
	}
}
